package com.atguigu.tingshu.order.api;

import com.atguigu.tingshu.common.anno.GuiGuLogin;
import com.atguigu.tingshu.common.result.Result;
import com.atguigu.tingshu.common.result.ResultCodeEnum;
import com.atguigu.tingshu.model.order.OrderInfo;
import com.atguigu.tingshu.order.service.OrderInfoService;
import com.atguigu.tingshu.order.template.SaveOrderTemplate;
import com.atguigu.tingshu.vo.order.OrderInfoVo;
import com.atguigu.tingshu.vo.order.TradeVo;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@Tag(name = "订单管理")
@RestController
@RequestMapping("api/order/orderInfo")
@SuppressWarnings({"unchecked", "rawtypes"})
public class OrderInfoApiController {

	private final OrderInfoService orderInfoService;

	@Autowired
	public OrderInfoApiController(OrderInfoService orderInfoService) {
		this.orderInfoService = orderInfoService;
	}
	/**
	 * @description: 确认订单
	 * @author Petrichor
	 * @date 2024/1/5 20:18
	 * @param tradeVo 订单确认对象
	 * @return orderInfoVo 订单对象
	**/
	@GuiGuLogin
	@Operation(summary = "确认订单")
	@PostMapping("/trade")
	public Result<OrderInfoVo> trade(@RequestBody @Validated TradeVo tradeVo) {
		OrderInfoVo orderInfoVo = orderInfoService.trade(tradeVo);
		return Result.ok(orderInfoVo);
	}
	/**
	 * @description: 提交订单
	 * @author Petrichor
	 * @date 2024/1/7 23:08
	 * @param orderInfoVo 订单对象
	 * @return 订单id
	**/
	@GuiGuLogin
	@Operation(summary = "提交订单")
	@PostMapping("submitOrder")
	public Result<Map<String, String>> submitOrder(@RequestBody @Validated OrderInfoVo orderInfoVo) {
		String orderNo = orderInfoService.submitOrder(orderInfoVo) ;
		Map<String ,String> map = new HashMap<>() ;
		map.put("orderNo" , orderNo) ;
		return Result.ok(map) ;
	}
	/**
	 * @description: 根据订单号获取订单信息
	 * @author Petrichor
	 * @date 2024/1/9 20:39
	 * @param orderNo 订单号
	 * @return 订单信息
	**/
	@GuiGuLogin
	@Operation(summary = "根据订单号获取订单信息")
	@GetMapping("getOrderInfo/{orderNo}")
	public Result<OrderInfo> getOrderInfo(@PathVariable String orderNo) {
		OrderInfo orderInfo = orderInfoService.getOrderInfoByOrderNo(orderNo);
		return Result.ok(orderInfo);
	}
	/**
	 * @description: 分页查询 获取用户订单
	 * @author Petrichor
	 * @date 2024/1/9 20:48
	 * @param page 当前页码
	 * @param limit 每页记录数
	 * @return 分页的订单数据
	**/
	@GuiGuLogin
	@Operation(summary = "获取用户订单")
	@GetMapping("findUserPage/{page}/{limit}")
	public Result<IPage<OrderInfo>> index(
			@Parameter(name = "page", description = "当前页码", required = true)
			@PathVariable Long page,
			@Parameter(name = "limit", description = "每页记录数", required = true)
			@PathVariable Long limit) {
		IPage<OrderInfo> pageModel = orderInfoService.findUserPage( page, limit);
		return Result.ok(pageModel);
	}
}

